/*
    created by hs on Feb.7, 2020
*/

export class PanLogic {
    private options: any;
    private lyr: any;
    private markerSymbol: any;
    private markerPosition: number[] = [];
    constructor(options: any) {
        this.options = options
        this.markerSymbol = new g2.sfs.PictureMarkerSymbol({
            source: this.options.symbolConfig.icons.markPoint,
            width: 32,
            height: 49,
            rotation: 0,
            scale: 1,
            opacity: 1,
            offsetX: 16,
            offsetY: 25
        });
    }

    public fit(districtCode: string) {
        const promise = new Promise((resolve, reject) => {
            const level = this.getDistrictLevel(districtCode);
            if (level === 0) {
                const extent = this.options.mapConfig.map.chinaExtent.extent;
                const envelope = new g2.sfs.Envelope(extent);
                this.options.map.pan(envelope, new Array(4).fill(100))
                resolve(extent);
            } else {
                this.options.service.getExtent(level, districtCode).then((res: any) => {
                    if (res && res.data && res.data.data && res.data.data.minx) {
                        const extent = res.data.data;
                        const envelope = new g2.sfs.Envelope(extent);
                        this.options.map.pan(envelope, new Array(4).fill(100))
                        resolve(extent)
                    } else {
                        reject('获取地图范围失败')
                    }

                });
            }
        })
        return promise;
    }

    public setPosition(center: any) {
        if (!this.lyr) { this.initPositionLayer(); }
        this.clear();
        if (!center) {
            return;
        }
        const point = new g2.sfs.Point({
            x: center[0] * 1,
            y: center[1] * 1,
            spatialReference: this.options.map.spatialReference
        })
        const ele = new g2.sfs.Element({
            geometry: point,
            symbol: this.markerSymbol
        });
        this.lyr.add(ele);
        this.markerPosition = [center[0] * 1, center[1] * 1];
    }

    public hitTest(point2: number[]) {
        const point1 = this.markerPosition;
        const lengthResult = point1.length === 2 && point2.length === 2;
        const xResult = Math.floor(point1[0] * 10000) === Math.floor(point2[0] * 10000);
        const yResult = Math.floor(point1[1] * 10000) === Math.floor(point2[1] * 10000);
        const result = lengthResult && xResult && yResult;
        return result;
    }

    public removePosition() {
        if (this.lyr) {
            this.lyr.clear();
        }
    }

    private initPositionLayer() {
        const layer = new g2.carto.ElementLayer({
            id: 'highlight_position_layer',
            name: '树状图点击高亮地区图层',
            zIndex: 10
        })
        this.options.map.addLayer(layer)
        this.lyr = layer
    }

    private clear() {
        this.lyr.clear();
        this.markerPosition = [];
    }

    /**
     * 根据行政区划编码获取行政区划等级
     * @param districtCode 行政区划编码
     */
    private getDistrictLevel(districtCode: string): number {

        // 0 -- 全国 ; 1 -- 省;2 -- 市;  3---区县
        // 直辖市按照市做特殊处理

        switch (true) {
            case /[0]{6}/.test(districtCode):
                return 0;
            case /(11|12|31|50)[0]{4}/.test(districtCode):
                return 2;
            case /[1-9]{2}[0]{4}/.test(districtCode):
                return 1;
            case /[1-9]{2}([1-9]\d{1}|\d{1}[1-9]){1}[0]{2}/.test(districtCode):
                return 2;
            default:
                return 3;
        }
    }
}
